
// Copyright (c) 2002 HTHK
package com.ffpc.sql;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;

/**
 * A Class class.
 * <P>
 * @author HTHK
 */
public abstract class ConcurrencyDataObject implements Table, ConcurrencyRow, Serializable
{
    private transient Connection con;
    protected String tableName;
    protected String timeStamp;
    private Hashtable columns = new Hashtable();
    private Key key;
    private UpdatableColumnsObject updatableColumnsObject;

    /**
      * Constructor
    */
    public ConcurrencyDataObject()
    {
        this.con = null;
        this.key = new Key();
        this.updatableColumnsObject = new UpdatableColumnsObject();
    }

    protected String retrieveColumnValue(String name)
    {

      Column column = this.getColumn(name);

        if (column==null)
          return "";
        else
        	//  System.out.println(name +"-> values:"+column.toString());
          return column.toString();

    }

    protected String setAttribute(Column column, String value)
    {
    	//System.out.println(column.getName()+"<>"+value);
      if (value!=null && value.trim().length()==0)
          value=null;
        //set column
        if (this.getColumn(column.getName())==null)
        {

        	//System.out.println("column.getName() is null but value = " +value);
            Column newColumn  = column.clone(null);
            newColumn.setValue(value);
            this.setColumn(newColumn);
        }
        else {
          this.getColumn(column.getName()).setValue(value);
          //System.out.println("column.getName() ="+column.getName()+" and value = " +value);
        }
        return value;
    }

//  The functions belongs to table interface

    @Override
	public void setConnection(Connection con)
    {
      this.con = con;
    }

    @Override
	public int insert()
      throws DataException
    {
    	ConcurBaseSqlCommand buc = new ConcurBaseSqlCommand(this.con);
      buc.insert(this);
      return 1;
    }

    @Override
	public int update() throws DataException
    {
    	try
    	{
    		ConcurBaseSqlCommand buc = new  ConcurBaseSqlCommand(this.con);
       	 	buc.update(this);
       	 	return 1;
    	}
    	catch(SQLException e)
    	{
    		e.printStackTrace();
    		throw new DataException(e); 
    	}

    }

    @Override
	public int delete() throws DataException
    {
    	try
    	{
    		ConcurBaseSqlCommand buc = new ConcurBaseSqlCommand(this.con);
    		buc.delete(this);
    		return 1;
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    		throw new DataException(e);
    	}
    }

//  The functions belongs to row interface

    @Override
	public void setColumn(Column column)
    {
        this.columns.put(column.getName(),column);
    }

    @Override
	public Column getColumn(String name)
    {
        return (Column)this.columns.get(name);
    }

    @Override
	public int size()
    {
        return this.columns.size();
    }

    @Override
	public void setColumns(Column[] cols)
    {
        if (columns!=null)
        {
          this.columns = new Hashtable();
          for (int i=0; i < cols.length; i++)
          {
              this.columns.put(cols[i].getName(),cols[i]);
          }
        }
        retrieveColumnValues();
    }

    @Override
	public Column[] getColumns()
    {
        if (this.columns==null || this.columns.size()==0)
            return null;
        else
        {
            return (Column[])this.columns.values().toArray(new Column[this.columns.size()]);
        }
    }

    @Override
	public PrimaryKey getPrimaryKey()
    {
        if (this.key==null)
            this.key = new Key();
        this.key.setTableName(getTableName());
        String[] list = getKeyColumnNames();
        this.key.setList(list);
        Column[] cols = null;
        if (list!=null && list.length>0 )
        {
            cols=new Column[list.length];
            for (int i=0;list!=null && i<list.length; i++)
            {
                cols[i] = this.getColumn(list[i]);
            }
        }
        this.key.setColumns(cols);
        return this.key;
    }

    @Override
	public UpdatableColumns getUpdatableColumns()
    {
        if (this.updatableColumnsObject==null)
            this.updatableColumnsObject = new UpdatableColumnsObject();
        String[] list = this.getUpdatableColumnNames();
        this.updatableColumnsObject.setList(list);
        Column[] cols = null;
        if (list!=null && list.length>0 )
        {
            cols=new Column[list.length];
//System.out.println("list length="+list.length);
            for (int i=0;list!=null && i<list.length; i++)
            {
//System.out.println(i+"="+list[i]);
                cols[i] = this.getColumn(list[i]);
            }
        }
        this.updatableColumnsObject.setColumns(cols);
        return this.updatableColumnsObject;
    }

    

    
    
    
    // abstract methods

    @Override
	public abstract String getTableName();

    protected abstract String[] getKeyColumnNames();

    protected abstract String[] getUpdatableColumnNames();

    protected abstract void retrieveColumnValues();
    
    
    //public abstract Column getTimeStamp();
    
    
    // ++++ add for IPS ++++
    //public abstract void setTimeStamp(String timeStamp);
    
   
    @Override
	public Column getTimeStamp()
    {
    	return getColumn(timeStamp);
    }
    
    @Override
	public String getTimeStampName()
    {
    	return timeStamp;
    }
    @Override
	public void setTimeStampName(String timeStamp)
    {
    	this.timeStamp = timeStamp;
    }
    
    /*public void setTimeStamp(String value)
    {
    	setAttribute(getTimeStamp(), value);
    }*/

    // ---- add for IPS ----
    

}



